package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTradeMarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/5/9 10:33
 * @Version 1.0
 */
@Service
public class BaseCategoryTradeMarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTradeMarkService {

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    /**
     * 据三级分类Id 获取到品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //  select * from base_category_trademark where category3_id = 61 and is_deleted = 0; # 1,2,3
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(trademarkLambdaQueryWrapper);

        //  获取到已绑定的品牌Id
        //        ArrayList<Long> longs = new ArrayList<>();
        //        for (BaseCategoryTrademark baseCategoryTrademark : baseCategoryTrademarkList) {
        //            longs.add( baseCategoryTrademark.getTrademarkId());
        //        }
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //  tmIdList = 1,2,3 复制小括号，写死右箭头，落地大括号
            //            List<Long> tmIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
            //                //     R apply(T t);
            //                return baseCategoryTrademark.getTrademarkId();
            //            }).collect(Collectors.toList());
            List<Long> tmIdList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //  根据tmIdList 查询 品牌列表
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(tmIdList);
            //  返回绑定的品牌列表
            return baseTrademarkList;
        }
        //  返回一个空数据
        return null;
    }

    @Override
    public void removeByCategory3Id(Long category3Id, Long trademarkId) {
        //  根据 category3Id trademarkId 删除;
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id)
                .eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        this.baseCategoryTrademarkMapper.delete(trademarkLambdaQueryWrapper);
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //  向数据库表写数据：base_category_trademark
        //  {"category3Id":85,"trademarkIdList":[1,2]}  85-1 85-2
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)){
            //  获取到添加对象的集合数据。
            List<BaseCategoryTrademark> trademarkList = trademarkIdList.stream().map(tmId -> {
                //  创建对象
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(tmId);
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            //  批量：
            this.saveBatch(trademarkList);
        }
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //  先通过三级分类Id 获取到已绑定的品牌Id
        //  select * from base_category_trademark where category3_id = 61 and is_deleted = 0; # 1,2,3
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(trademarkLambdaQueryWrapper);

        //  判断
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //  有绑定关系,先获取到绑定id 1,2,3
            List<Long> tmIdList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //  获取到除了绑定的数据。 5,6 品牌数据.
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null).stream().filter(baseTrademark -> !tmIdList.contains(baseTrademark.getId())).collect(Collectors.toList());
            //  返回品牌数据
            return baseTrademarkList;
        }
        //  否则就是没有绑定关系。 select * from base_trademark;
        return baseTrademarkMapper.selectList(null);
    }
}
